const mysql = require('mysql2');

const getConnection = () => {
    const connection = mysql.createConnection({
        host: '192.168.1.25',
        port: 33061,
        user: 'root',
        password: '1qaz2wsx3edc4rfv',
        database: 'salesforce_exam'
    });
    connection.connect();
    return connection;
}

const createQuestions = (questions) => {
    let connection = getConnection();
    const sqlStr = "INSERT INTO Questions (QuestionText, Anwser, OptionA, OptionB, OptionC, OptionD, OptionE, OptionF, QuestionTypeID, Subject) VALUES ?";
    const values = questions.map(x => [x.QuestionText, x.Anwser, x.OptionA, x.OptionB, x.OptionC, x.OptionD, x.OptionE, x.OptionF, x.QuestionTypeID, x.Subject]);
    connection.query(sqlStr, [values], (err, results) => {
        if (err) return console.log(err.message)

        if (results.affectedRows >= 1) {
            console.log('插入数据成功!')
        }
    });

    connection.end();
}

const createExam = (exam) => {
    console.log(exam);
    let connection = getConnection();
    const sqlStr = "INSERT INTO ExaminationSetups (Title, StrTime, EndTime, CourseEnum, DXNumer, DXScore, DSXNumer, DSXScore, PDNumer, PDScore, TestTime, ParticipationTypes, Subject) VALUES ?";
    const value = [exam.title, "2000-01-01", "2099-12-31", 0, exam.singleCount, exam.singleCount, exam.multipleCount, exam.multipleCount, 0, 0, 60, 1, exam.subject];
    connection.query(sqlStr, [[value]], (err, results) => {
        if (err) return console.log(err.message)

        if (results.affectedRows >= 1) {
            console.log('插入数据成功!')
        }
    });

    connection.end();
}

module.exports = {
    createQuestions,
    createExam
}